package com.toolkit.util;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    /**
     * 当前日期增加到指定增加的天数，时分秒设置为00:00:00
     * @param nowDate
     * @param addDays
     * @return 2023-1-11 00:00:00
     */
    public static Date addDays(Date nowDate, Integer addDays){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowDate);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        LocalDateTime nowDateTime = LocalDateTime.of(year, month, day + addDays, 0, 0, 0);
        Instant instant = Timestamp.valueOf(nowDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))).toInstant();
        return Date.from(instant);
    }
    /**
     * 这里是根据日期获得日期字符串-马艳东
     * @param date
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getLongDate(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = null;
        if(date!=null){
            dateStr = sdf.format(date);
        }
        return dateStr;
    }
}
